home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / term-source.lha / Speech.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  3KB  |  169 lines

  1. /*
  2. **    Speech.c
  3. **
  4. **    Speech support routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.     /* Local symbols. */
  17.  
  18. STATIC UBYTE SpeechString[512],TranslatedString[512];
  19. STATIC struct narrator_rb *NarratorRequest;
  20. STATIC struct MsgPort *NarratorPort;
  21. STATIC BOOL DidSpeak;
  22.  
  23.     /* DeleteSpeech():
  24.      *
  25.      *    Free the data associated with the speech function.
  26.      */
  27.  
  28. VOID
  29. DeleteSpeech()
  30. {
  31.     if(DidSpeak)
  32.     {
  33.         if(!CheckIO((struct IORequest *)NarratorRequest))
  34.             WaitIO((struct IORequest *)NarratorRequest);
  35.     }
  36.  
  37.     if(NarratorRequest)
  38.     {
  39.         if(NarratorRequest->message.io_Device)
  40.             CloseDevice((struct IORequest *)NarratorRequest);
  41.  
  42.         DeleteIORequest((struct IORequest *)NarratorRequest);
  43.         NarratorRequest = NULL;
  44.     }
  45.  
  46.     if(NarratorPort)
  47.     {
  48.         DeleteMsgPort(NarratorPort);
  49.         NarratorPort = NULL;
  50.     }
  51.  
  52.     if(TranslatorBase)
  53.     {
  54.         CloseLibrary(TranslatorBase);
  55.         TranslatorBase = NULL;
  56.     }
  57.  
  58.     DidSpeak = FALSE;
  59. }
  60.  
  61.     /* CreateSpeech():
  62.      *
  63.      *    Open translator.library and narrator.device, perform
  64.      *    the necessary setups for the speech function.
  65.      */
  66.  
  67. BOOL
  68. CreateSpeech()
  69. {
  70.     if(TranslatorBase = OpenLibrary("translator.library",0))
  71.     {
  72.         if(NarratorPort = CreateMsgPort())
  73.         {
  74.             if(NarratorRequest = (struct narrator_rb *)CreateIORequest(NarratorPort,sizeof(struct narrator_rb)))
  75.             {
  76.                 STATIC UBYTE AnyChannel[] =
  77.                 {
  78.                     LEFT0F,
  79.                     LEFT1F,
  80.                     RIGHT0F,
  81.                     RIGHT1F
  82.                 };
  83.  
  84.                     /* Any channel will do. */
  85.  
  86.                 NarratorRequest->ch_masks            = AnyChannel;
  87.                 NarratorRequest->nm_masks            = sizeof(AnyChannel);
  88.  
  89.                     /* This is a write request. */
  90.  
  91.                 NarratorRequest->message.io_Command    = CMD_WRITE;
  92.                 NarratorRequest->message.io_Data    = (APTR)SpeechString;
  93.  
  94.                 if(!OpenDevice("narrator.device",0,(struct IORequest *)NarratorRequest,0))
  95.                 {
  96.                     SpeechSetup();
  97.  
  98.                     return(TRUE);
  99.                 }
  100.             }
  101.         }
  102.     }
  103.  
  104.     DeleteSpeech();
  105.  
  106.     return(FALSE);
  107. }
  108.  
  109.     /* Say(STRPTR String,...):
  110.      *
  111.      *    Translate a string into phonemes and speak it.
  112.      */
  113.  
  114. VOID
  115. Say(STRPTR String,...)
  116. {
  117.     if(SpeechConfig.Enabled && English)
  118.     {
  119.         if(!TranslatorBase)
  120.             CreateSpeech();
  121.  
  122.         if(TranslatorBase)
  123.         {
  124.             va_list VarArgs;
  125.  
  126.             if(DidSpeak)
  127.             {
  128.                 if(!CheckIO((struct IORequest *)NarratorRequest))
  129.                     WaitIO((struct IORequest *)NarratorRequest);
  130.             }
  131.  
  132.             va_start(VarArgs,String);
  133.             LimitedVSPrintf(sizeof(TranslatedString),TranslatedString,String,VarArgs);
  134.             va_end(VarArgs);
  135.  
  136.             if(!Translate(TranslatedString,strlen(TranslatedString),SpeechString,511))
  137.             {
  138.                 NarratorRequest->message.io_Length = strlen(SpeechString);
  139.  
  140.                 ClrSignal(PORTMASK(NarratorRequest->message.io_Message.mn_ReplyPort));
  141.  
  142.                 SendIO((struct IORequest *)NarratorRequest);
  143.  
  144.                 DidSpeak = TRUE;
  145.             }
  146.             else
  147.                 DidSpeak = FALSE;
  148.         }
  149.     }
  150. }
  151.  
  152.     /* SpeechSetup():
  153.      *
  154.      *    Transfer the configuration data into the setup.
  155.      */
  156.  
  157. VOID
  158. SpeechSetup()
  159. {
  160.     if(NarratorRequest)
  161.     {
  162.         NarratorRequest->rate        = SpeechConfig.Rate;
  163.         NarratorRequest->pitch        = SpeechConfig.Pitch;
  164.         NarratorRequest->sex        = SpeechConfig.Sex;
  165.         NarratorRequest->volume        = SpeechConfig.Volume;
  166.         NarratorRequest->sampfreq    = SpeechConfig.Frequency;
  167.     }
  168. }
  169.